import java.awt.Color;
import java.awt.image.BufferedImage;
import java.awt.image.RescaleOp;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

public class Cubie{
	CubieColor cubeColor; //cubeColor is of the enumeration CubieColor
	int cube;
	float imageHeight, imageWidth;
	int xCoordinate, yCoordinate;
	//This is the path of the file, seems to only work with jpg
	File image = new File("C:/users/joey/desktop/sides/cube.jpg");
	public static BufferedImage img = null;
	int red, green, blue, alpha;
	public Cubie(double xScale, double yScale){
		try{
			img = ImageIO.read(image);
		}catch (IOException e){ //This exceptions is thrown when the file isn't found
			System.out.println("Catastrophic Error: IOException thrown");
		}
		
		if(img != null){
			imageHeight = img.getHeight();
			imageWidth = img.getWidth();
			//effectively this if/else simply checks to see if the image has been rotated and compesates
			//assuming that the one calling this function has plugged in a scale factor for an image with
			//a greater width
			if(imageHeight > imageWidth){
				xCoordinate = (int)(imageWidth * yScale);
				yCoordinate = (int)(imageHeight * xScale);
			}else{
				xCoordinate = (int)(imageWidth * xScale);
				yCoordinate = (int)(imageHeight * yScale);
			}
			
			cube = img.getRGB(xCoordinate, yCoordinate);
			//Here we grab the specific numbers from the 4 byte ARGB integer ie: 1-255 stored in bits
			//bits 0-7 are blue, 8-15 are green, 16-23 are red, and 24-31 are alpha channel quantity
			//alpha channel is basically opacity but is ignored in this situation
			alpha = (cube >> 24) & 0xFF;
			red = (cube >> 16) & 0xFF;
			green = (cube >> 8) & 0xFF;
			blue = (cube ) & 0xFF;
		}else{
			System.out.println("Catastrophic error");
		}
	}
	
	
//	RescaleOp res = new RescaleOp(2f,15,null);
	
	//The following functons ie: returnRed(), returnGreen(), and returnBlue() are simply returning
	//the RGB values from the 4 byte regular integer
	public int returnRed(){
		if(img != null){
			return red;
		}else{
			return 1;
		}
	}
	
	public int returnGreen(){
		if(img != null){
			return green;
		}else{
			
			return 1;
		}
	}
	
	public int returnBlue(){
		if(img != null){
			return blue;
		}else{
			return 1;
		}
	}
	
	//This function uses arbitrary ratios and quantities of red green and blue, the equations and 
	//conditions weren't derived from something special
	public CubieColor returnColor(){
		int red, green, blue;
		CubieColor cubie;
		red = this.returnRed();
		green = this.returnGreen();
		blue = this.returnBlue();
//		System.out.println(xCoordinate);
//		System.out.println(yCoordinate);
		
		if(red == 1 && blue == 1 && green == 1){
			return cubie = CubieColor.none;
		}
		
		if(red > green && red > blue){
			if(((red - green) >= 80 && (red - blue) > 100) && (red - green) <= 160){
				return cubie = CubieColor.orange;
			}else if(red > (green + blue) * 2.2){
				return cubie = CubieColor.red;
			}else if(((red - green) < 95 && (red - blue) > 100)){
				return cubie = CubieColor.yellow;
			}else if(red > 150 && blue < 30 && green < 100){
				return cubie = CubieColor.red;
			}
		}
		
		if(green > (red + blue)){
			return cubie = CubieColor.green;
		}
		
		if(blue > (red + green)){
			return cubie = CubieColor.blue;
		}
		if(red > 140 && green > 140 && blue > 140){
			if(red - (blue + green) <= 50){
				if(green - (red + blue) <= 50){
					if(blue - (red + green) <= 50){
						return cubie = CubieColor.white;
					}
				}
			}
		}
		
		if(red > 150 && green > 150 && blue < 70){
			if(Math.abs(red - green) < 50){
				return cubie = CubieColor.yellow;
			}
		}
		
		return cubie = CubieColor.none;
	}
	
	public int returnWidth(){
		if(img != null){
			return (int)img.getWidth();
		}else{
			return 20;
		}
	}
	
	public int returnHeight(){
		if(img != null){
			return (int)img.getHeight();
		}else{
			return 20;
		}
	}
	
}